package com.xiaotao.mq.mq;

import com.xiaotao.mq.config.DelayedMessageConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 消息发送者
 * @author 16082
 */
@Slf4j
@Service
public class MQSender {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void testSend(String msg, Integer delayTime) {
        log.info("测试发送延时消息 {} : {}", delayTime, msg);
        //将消息携带路由键值
        rabbitTemplate.convertAndSend(
                //交换机名称
                DelayedMessageConfig.DELAYED_EXCHANGE_NAME,
                //路由 key
                DelayedMessageConfig.DELAYED_ROUTING_KEY,
                msg,
                message -> {
                    //设置延时的时间  单位毫秒
                    message.getMessageProperties().setDelay(delayTime);
                    return message;
                });
    }
}
